import Taro, { Component } from '@tarojs/taro'
import { View, Button, Text } from '@tarojs/components'
import { AtToast } from "taro-ui"
import { post } from '../../service/api'
import './index.scss'

class Company extends Component {
  constructor() {
    super(...arguments)
    this.state = {
      isShowToast: false,
      code: "",
      disabled: false,
      msg: ""
    }
  }
  config = {
    navigationBarTitleText: '填写邀请码',
  }
  pop(msg) {
    this.setState({
      isShowToast: true,
      msg: msg
    }, () => {
      setTimeout(() => {
        this.setState({
          isShowToast: false
        })
      }, 1500)
    })
  }
  getCode(e) {

    this.setState({
      code: e.detail.value,
      disabled: e.detail.value
    })
  }
  async smHandle() {
    await Taro.scanCode({
      success: (res) => {
        this.setState({
          code: res.result,
          disabled: true
        })
      }
    })

  }
  toPageHome() {
    Taro.switchTab({ url: `/pages/index/index` })
  }
  async inputCode() {
    const { code } = this.state,
      { data } = await post('pserson/perfect', {
        inviteCode: code
      })
    if (data.code == 0) {
      Taro.switchTab({ url: `/pages/index/index` })
    } else {
      this.pop("邀请码输入有误，请重试")
    }
  }
  render() {
    const { disabled, isShowToast, msg, code } = this.state
    return (
      <View className='invitation-code'>
        <View className='i-code'>
          <View className="input-box">
            <View className="icon-box">
              <Image className="img1" src={require("../../asset/imgs/yqm.png")} />
            </View>
            <Input className="code" type='number' value={code} onInput={this.getCode} maxLength='11' placeholder="请输入邀请码 ( 选填 )" placeholder-style="color:#ccc;" />
            <Image className="sm" onClick={this.smHandle.bind(this)} src={require("../../asset/imgs/sm.png")} />
          </View>
          <View className={disabled ? "confirm confirm-btn" : "confirm"} onClick={this.inputCode.bind(this)}>确定</View>
          <View className="pass" onClick={this.toPageHome.bind(this)}>跳过</View>
          <AtToast
            isOpened={isShowToast}
            text={msg}
            duration={2000}
          ></AtToast>
        </View>
      </View>
    )
  }
}

export default Company
